package com.moon.game.manager;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Logger;

import com.moon.game.resource.model.MainLand;
import com.moon.game.resource.model.MainLandPorts;
import com.moon.game.resource.model.Npcs;

/**
 * @description 大陆加载
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-5-9 下午4:57:10
 */
public class MainLandMG {
	private MainLandMG() {
	}
	public static MainLandMG instance = new MainLandMG();
	private static final Logger log = Logger.getLogger(MainLandMG.class);
	public static ConcurrentHashMap<Integer,MainLand> mainLandMap = new ConcurrentHashMap<Integer, MainLand>();
	public static ConcurrentHashMap<Integer, MainLand> getMainLandMap() {
		return mainLandMap;
	}
	public static ConcurrentHashMap<Integer,ArrayList<Integer>> mainLandPorsMap = new ConcurrentHashMap<Integer, ArrayList<Integer>>();
	public static ConcurrentHashMap<Integer, ArrayList<Integer>> getMainLandPorsMap() {
		return mainLandPorsMap;
	}

	public void init() {
		loadMainLands();
		loadPorts();
	}
	/**
	 * 大陆对应的npc
	 */
	public static ConcurrentHashMap<Integer,ArrayList<Npcs>> npcList = new ConcurrentHashMap<Integer, ArrayList<Npcs>>();
	
	/**
	 * 获取该大陆对应的npc列表
	 * 
	 * @param mainLandId
	 * @return
	 */
	public final ArrayList<Npcs> getNpcList(int mainLandId) {
		return npcList.get(mainLandId);
	}

	/**
	 * 根据id获取大陆对象
	 * 
	 * @param id
	 * @return
	 */
	public final MainLand getMainLand(int id) {
		return mainLandMap.get(id);
	}

	/**
	 * 根据id获得大陆通道口
	 * 
	 * @param id
	 * @return
	 */
	public final ArrayList<Integer> getMainLandPors(int id) {
		return mainLandPorsMap.get(id);
	}

	
	private void loadMainLands() {
		List<MainLand> mainLands = DBManager.queryAllMainLand();
		for (MainLand mainLand : mainLands) {
			if (mainLandMap.get(mainLand.getId()) == null) {
				mainLandMap.put(mainLand.getId(), mainLand);
				//加载npc
				if (mainLand.getNpcs() != null) {
					String[] cell = mainLand.getNpcs().split("[|]");
					if (cell != null && cell.length > 0) {
						ArrayList<Npcs> npcs = new ArrayList<Npcs>();
						for (String npcId:cell) {
							Npcs npc = NPCMG.instance.getNpcs(Integer.parseInt(npcId));
							npcs.add(npc);
						}
						npcList.put(mainLand.getId(), npcs);
					}
				}
			}else {
				log.error("mainland id has conflict...");
			}
		}
	}
	private void loadPorts() {
		List<MainLandPorts> ports = DBManager.queryAllMainLandPorts();
		for (MainLandPorts port : ports) {
			if (mainLandPorsMap.get(port.getId()) == null) {
				ArrayList<Integer> portList = new ArrayList<Integer>();
				String[] cell = port.getPorts().split("[|]");
				if (cell != null) 	
					for (int i = 0; i < cell.length; i++) {
						portList.add(Integer.parseInt(cell[i]));
					}
				mainLandPorsMap.put(port.getId(), portList);
			}else {
				log.error("mainlandports id has conflict...");
			}
		}
	}
}
